package com.yhn.init.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhn.init.common.ErrorCode;
import com.yhn.init.exception.BusinessException;
import com.yhn.init.exception.ThrowUtils;
import com.yhn.init.mapper.PurchaseMapper;
import com.yhn.init.model.dto.purchase.PurchaseAddRequest;
import com.yhn.init.model.entity.Purchase;
import com.yhn.init.model.enums.PurchaseStatusEnum;
import com.yhn.init.service.PurchaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 86177
 * @description 针对表【purchase】的数据库操作Service实现
 * @createDate 2024-03-22 09:49:49
 */
@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase>
        implements PurchaseService {

    @Override
    @Transactional
    public Long addPurchase(PurchaseAddRequest purchaseAddRequest) {
        if (purchaseAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Purchase purchase = new Purchase();
        BeanUtil.copyProperties(purchaseAddRequest, purchase);
        purchase.setStatus(PurchaseStatusEnum.CREATED.getValue());
        ThrowUtils.throwIf(!this.save(purchase), ErrorCode.SYSTEM_ERROR);
        return purchase.getId();
    }
}




